package com.vip.file.controller;


import com.vip.file.config.SystemException;
import com.vip.file.model.entity.Files;
import com.vip.file.model.response.Result;
import com.vip.file.service.IFileService;
import com.vip.file.utils.FileTypeCheckerUtils;
import com.vip.file.utils.TextFileReader;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**
 * 页面预览
 */
@RestController
@RequestMapping("/page")
@Slf4j
@RequiredArgsConstructor
public class PageController {

    private final IFileService fileService;

    /**
     * 新页面拼接输出   前端直接open这个地址
     * @return
     */
    @GetMapping("/preview/{id}")
    public String logout(@PathVariable String id) {
        Result<Files> fileDetails = fileService.getFileDetails(id);
        if (!fileDetails.isSuccess()) {
            throw new SystemException(fileDetails.getErrorCode().getCode(), fileDetails.getDescription());
        }

        // InetAddress localhost = null;
        String ip = "120.46.220.220";
        // String ip = "127.0.0.1";
        // try {
        //     localhost = InetAddress.getLocalHost();
        //     ip = localhost.getHostAddress();
        //     log.info("IP Address:{} " + ip);
        // } catch (UnknownHostException e) {
        //     log.error(e.getMessage());
        // }

        //获取文件后缀判断是图片还是视频
        String suffix = fileDetails.getData().getSuffix();
        if (FileTypeCheckerUtils.isImage(suffix)) {
            StringBuilder sb = new StringBuilder();
            sb.append("<h2>图片预览</h2>")
                    .append("<img src=\"http://"+ip+"/files/"+fileDetails.getData().getFileName()+ "\"  style=\"max-width: 100%; max-height: 100%;\">");
            return sb.toString();
        }

        if (FileTypeCheckerUtils.isVideo(suffix)) {
            StringBuilder sb = new StringBuilder();
            sb.append("<h2>视频预览</h2>")
                    .append("<video width=\"600\" height=\"auto\" controls>")
                    .append("<source src=\"http://"+ip+"/files/"+fileDetails.getData().getFileName()+ "\" type=\"video/mp4\">")
                    .append("  抱歉，您的浏览器不支持显示视频。</video>");
            return sb.toString();
        }

        if (FileTypeCheckerUtils.isMp3(suffix)) {
            StringBuilder sb = new StringBuilder();
            sb.append("<h2>音乐播放</h2>")
                    .append("<audio src=\"http://"+ip+"/files/"+fileDetails.getData().getFileName()+ "\" type=\"video/mp4\" controls>")
                    .append("  抱歉，您的浏览器不支持显示视频。</video>");
            return sb.toString();
        }

        if (FileTypeCheckerUtils.isTxt(suffix)) {
            String sb = null;
            try {
                sb = TextFileReader.readTxtFileFromUrl("http://" + ip + "/files/" + fileDetails.getData().getFileName());
            } catch (IOException e) {
                e.printStackTrace();
                return "文件url读取失败";
            }
            return sb;
        }

        //拿到文件的id
        return "<h2>不是视频/图片,所以我不想给你加载<h2>";
    }

}
